package com.sinodata.bsm.center.service.notify;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.NotifyRuleBean;
import com.sinodata.bsm.center.cache.NotifyExcludeResRuleCache;
import com.sinodata.bsm.center.cache.NotifyRuleCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.impl.NotifyDaoImpl;
import com.sinodata.bsm.center.dao.impl.NotifyRuleDaoImpl;
import com.sinodata.bsm.common.constants.NotifyConstants;
import com.sinodata.bsm.common.vo.Notify;
import com.sinodata.bsm.common.vo.NotifyEventTypeRule;
import com.sinodata.bsm.common.vo.NotifyExcludeResRule;
import com.sinodata.bsm.common.vo.NotifyRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-3 PM 3:57:56      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class NotifyRuleService {

    @Autowired
    private NotifyRuleCache notifyRuleCache;

    @Autowired
    private NotifyExcludeResRuleCache notifyExcludeResRuleCache;

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private NotifyRuleDaoImpl notifyRuleDao;

    @Autowired
    private NotifyDaoImpl notifyDao;

    /**
     * 查询资源和资源继承的事件通知规则
     * 
     * @param resId
     * @return
     */
    public List<NotifyRuleBean> findAllByRes(Long resId) {
        List<NotifyRuleBean> notifyRuleBeans = new ArrayList<NotifyRuleBean>();
        //查询自身的,从缓存中获取
        List<NotifyRule> list = notifyRuleCache.getAllByRes(resId);
        for (NotifyRule notifyRule : list) {
            notifyRuleBeans.add(NotifyRuleBean.get(notifyRule.getId()));
        }
        return notifyRuleBeans;
    }

    /**
     * 禁用事件通知规则，不更新缓存，Action层需要单独更新缓存数据
     * 
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void disable(Long id) {
        NotifyRule notifyRule = notifyRuleCache.get(id);
        notifyRule.setStatus(NotifyConstants.RULE_STATUS_DISABLE);
        baseDao.update(notifyRule);

        //add by dongyushi  通知规则被禁用  应该停止继续通知
        List<Notify> notifyList = notifyDao.findByNotifyRuleId(id);
        resume(notifyList);
    }

    /**
     * 取消通知
     * 
     * @param list
     */
    private void resume(List<Notify> list) {
        Long[] ids = new Long[list.size()];
        int i = 0;
        for (Notify notify : list) {
            ids[i] = notify.getId();
            i++;
        }
        notifyDao.batchDelete(ids);
    }

    /**
     * 启用事件通知规则，不更新缓存，Action层需要单独更新缓存数据
     * 
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void enable(Long id) {
        NotifyRule notifyRule = notifyRuleCache.get(id);
        notifyRule.setStatus(NotifyConstants.RULE_STATUS_ENABLE);
        baseDao.update(notifyRule);
    }

    /**
     * 屏蔽事件通知规则，不更新缓存
     * 
     * @param notifyRuleId
     * @param resId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public NotifyExcludeResRule exclude(Long notifyRuleId, Long resId) {
        NotifyExcludeResRule notifyExcludeResRule = new NotifyExcludeResRule();
        notifyExcludeResRule.setNotifyRuleId(notifyRuleId);
        notifyExcludeResRule.setResId(resId);
        baseDao.save(notifyExcludeResRule);
        return notifyExcludeResRule;
    }

    /**
     * 解除屏蔽事件通知规则，不更新缓存
     * 
     * @param notifyRuleId
     * @param resId
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void include(Long notifyRuleId, Long resId) {
        NotifyExcludeResRule notifyExcludeResRule = notifyExcludeResRuleCache.getByNotifyRuleAndRes(notifyRuleId, resId);
        if (notifyExcludeResRule != null) {
            baseDao.delete(notifyExcludeResRule);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void add(NotifyRule notifyRule) {
        baseDao.save(notifyRule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addEventType(List<NotifyEventTypeRule> list) throws Exception {
        baseDao.batchSave(list);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delEventType(List<NotifyEventTypeRule> list) throws Exception {
        Long[] ids = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ids[i] = list.get(i).getId();
        }
        notifyRuleDao.batchDeleteNotifyEventTypeRule(ids);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void update(NotifyRule notifyRule) {
        baseDao.update(notifyRule);
    }

    /**
     * 删除通知规则，同时删除相关规则
     * 
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Long id) {
        notifyRuleDao.deleteNotifyReceiveRule(id);
        notifyRuleDao.deleteNotifyPauseRule(id);
        notifyRuleDao.deleteNotifyExcludeResRule(id);
        notifyRuleDao.deleteNotifyEventTypeRule(id);
        notifyRuleDao.deleteNotifyDelayRule(id);
        notifyRuleDao.delete(id);
    }

    public boolean checkName(Long resId, String name) {
        if (notifyRuleDao.getByNameAndResId(resId, name) > 0) {
            return false;
        } else {
            return true;
        }
    }
}